<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="标识符" prop="id">
        <el-input v-model="dataForm.id" placeholder="标识符"></el-input>
      </el-form-item>
      <el-form-item label="楼栋名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="楼栋名称"></el-input>
      </el-form-item>
      <el-form-item label="单元数" prop="unitNum">
        <el-input v-model="dataForm.unitNum" placeholder="单元数"></el-input>
      </el-form-item>
      <el-form-item label="层数" prop="floorNum">
        <el-input v-model="dataForm.floorNum" placeholder="层数"></el-input>
      </el-form-item>
      <el-form-item label="房屋总数" prop="totalHouses">
        <el-input v-model="dataForm.totalHouses" placeholder="房屋总数"></el-input>
      </el-form-item>
      <el-form-item label="已选房屋数" prop="selectedHouses">
        <el-input v-model="dataForm.selectedHouses" placeholder="已选房屋数"></el-input>
      </el-form-item>
      <el-form-item label="楼栋图片URL地址" prop="imageUrl">
        <el-input v-model="dataForm.imageUrl" placeholder="楼栋图片URL地址"></el-input>
      </el-form-item>
      <el-form-item label="创建者" prop="creator">
        <el-input v-model="dataForm.creator" placeholder="创建者"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="修改者" prop="updater">
        <el-input v-model="dataForm.updater" placeholder="修改者"></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateDate">
        <el-input v-model="dataForm.updateDate" placeholder="修改时间"></el-input>
      </el-form-item>
      <el-form-item label="状态 0：启用 1：删除" prop="delFlag">
        <el-input v-model="dataForm.delFlag" placeholder="状态 0：启用 1：删除"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  name: "",
  unitNum: "",
  floorNum: "",
  totalHouses: "",
  selectedHouses: "",
  imageUrl: "",
  creator: "",
  createDate: "",
  updater: "",
  updateDate: "",
  delFlag: "",
});

const rules = ref({
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/rental/areabuilding/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/rental/areabuilding", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>